#include <QtGui>
#include "restreminder.h"

#if defined(Q_OS_WIN)
HHOOK mouse_hook;
HHOOK keyboard_hook;
QElapsedTimer* KRestReminder::sm_workTotalTime = new QElapsedTimer;
QElapsedTimer* KRestReminder::sm_restTotalTime = new QElapsedTimer;
#endif

#if defined(Q_OS_WIN)
LRESULT CALLBACK KRestReminder::LowLevelMouseProc(_In_ int nCode, 
	_In_ WPARAM wParam,
	_In_ LPARAM lParam
	)
{
//	qDebug()<<"keyboard work correct"<<endl;
	sm_restTotalTime->start();
	return CallNextHookEx(keyboard_hook, nCode, wParam, lParam);
}

LRESULT CALLBACK KRestReminder::LowLevelKeyboardProc(
	_In_  int nCode,
	_In_  WPARAM wParam,
	_In_  LPARAM lParam
	)
{
//	qDebug()<<"mouse work correct"<<endl;
	sm_restTotalTime->start();
	return CallNextHookEx(mouse_hook, nCode, wParam, lParam);
}
#endif

KRestReminder::KRestReminder(QSystemTrayIcon *trayIcon, QWidget *parent) : QWidget(parent)
{
	this->m_trayIcon = trayIcon;
	this->m_showStartUpMessage = true;

#if defined(Q_OS_LINUX)
	QSettings settings("Kingsoft", "wpshelper");
	m_restAmountTime = settings.value("time/Rest_Amount_Time", DEFAULT_REST_AMOUNT_TIME).toInt();
	m_workAmountTime = settings.value("time/Work_Amount_Time", 10).toInt();

	qDebug() << "rest: " << m_restAmountTime << " work: " << m_workAmountTime << endl;
	qDebug() << "class name: " << this->metaObject()->className();
	m_maintainThread = new KMaintainWorkTimestampsThread(&m_startWorkTimestamp, &m_stopWorkTimestamp);
	m_maintainThread->start();
#elif defined(Q_OS_WIN)
	m_restAmountTime = 10;
//	m_workAmountTime = 15;
#endif
}

KRestReminder::~KRestReminder()
{
//	qDebug()<<"~function() is action"<<endl;
#if defined(Q_OS_WIN)
    UnhookWindowsHookEx(keyboard_hook);
    UnhookWindowsHookEx(mouse_hook);
	delete sm_workTotalTime;
	delete sm_restTotalTime;
#endif
	delete m_timer;
	delete m_trayIcon;
}

int KRestReminder::getWorkAmountTime()
{
	return m_workAmountTime;
}

void KRestReminder::setWorkAmountTime(int workAmountTime)
{
	m_workAmountTime = workAmountTime;
	m_startWorkTimestamp = -1;
	m_stopWorkTimestamp = -1;
}

void KRestReminder::detectTimeout()
{
	if (m_showStartUpMessage)
	{
		m_trayIcon->showMessage(QApplication::applicationName(), tr("WPS Helper started."));
	}

	m_showStartUpMessage = false;

#if defined(Q_OS_LINUX)
	int currentTimestamp = time(0);
	int workTotal = currentTimestamp - m_startWorkTimestamp;
	int restTotal = currentTimestamp - m_stopWorkTimestamp;

	qDebug() << "work total: " << workTotal;
	qDebug() << "rest total: " << restTotal;
	if (m_startWorkTimestamp == -1)
	{
		return;
	}
	
	if (restTotal > m_restAmountTime * TIME_UNIT)
	{
		qDebug() << "ok, you can work again";
		m_startWorkTimestamp = -1;
	} else if (workTotal >= m_workAmountTime * TIME_UNIT) {
		qDebug() << "you should rest now";
		if (workTotal == m_workAmountTime * TIME_UNIT)
		{
			m_trayIcon->showMessage(QApplication::applicationName(), tr("You are working too hard, why not go to rest for a while."));
		} else if (workTotal - m_workAmountTime * TIME_UNIT == DEFAULT_REMIND_INTERVAL * TIME_UNIT && restTotal < DEFAULT_REMIND_INTERVAL * TIME_UNIT)
		{
			m_trayIcon->showMessage(QApplication::applicationName(), tr("It seems you are so busy today, if this balloon message annoys you, you can click the system tray icon \"Quit\" to avoid it."));
		} else if (workTotal - m_workAmountTime * TIME_UNIT == (DEFAULT_REMIND_INTERVAL * 2) * TIME_UNIT && restTotal < DEFAULT_REMIND_INTERVAL * TIME_UNIT)
		{
			m_trayIcon->showMessage(QApplication::applicationName(), tr("This reminder will no longer remind you today, sorry for bothering you, have a nice day. ^_^"));
		}
		
	}
#elif defined(Q_OS_WIN)
	if(sm_restTotalTime->elapsed() > m_restAmountTime * 10000)
	{
		sm_restTotalTime->start();
		sm_workTotalTime->start();
	}
	else
	{
		if(sm_workTotalTime->elapsed() > m_workAmountTime *1000  &&sm_workTotalTime->elapsed() < m_workAmountTime *1000  +1000)
	    {
		    m_trayIcon->showMessage(QString("WPS Tips"),QString("You are working too hard, why not go to rest for a while"));
	    }
		else if(sm_workTotalTime->elapsed() > m_workAmountTime *1000+10000 && sm_workTotalTime->elapsed() < m_workAmountTime *1000 + 11000)
		{
			m_trayIcon->showMessage(QString("WPS Tips"),QString("Another one minuter passed, please keep away to your computer, have a rest and keep your energy"));
		}
		else if(sm_workTotalTime->elapsed() > m_workAmountTime *1000 +20000)// && sm_workTotalTime->elapsed() < m_workAmountTime+ 21000)
		{
			m_trayIcon->showMessage(QString("WPS Tips"),QString("you must get away from computer now ,so you can keep your health ,and improve work efficiency"));
			m_timer->stop();
		}
	 }
#endif
}

void KRestReminder::start()
{
#if defined(Q_OS_LINUX)
#elif defined(Q_OS_WIN)
	sm_workTotalTime->start();
	sm_restTotalTime->start();
	mouse_hook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, GetModuleHandle(NULL), 0);
	keyboard_hook = SetWindowsHookEx(WH_MOUSE_LL, LowLevelMouseProc,GetModuleHandle(NULL), 0 );
#endif
	m_timer = new QTimer;
	connect(m_timer, SIGNAL(timeout()), this, SLOT(detectTimeout()));
	m_timer->start(1000);
}

void KRestReminder::restart()
{
}

